package com.d3works.apps.account.web;

import com.d3works.apps.account.db.PayVoucherElementToPay;
import com.d3works.apps.account.service.payment.paymentvoucher.PayVoucherElementToPayService;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;

@Singleton
public class PayVoucherElementToPayController {

  public void updateAmountToPayCurrency(ActionRequest request, ActionResponse response) {
    try {
      PayVoucherElementToPay elementToPayContext =
          request.getContext().asType(PayVoucherElementToPay.class);

      Beans.get(PayVoucherElementToPayService.class).updateAmountToPayCurrency(elementToPayContext);

      response.setValue("amountToPayCurrency", elementToPayContext.getAmountToPayCurrency());
      response.setValue(
          "remainingAmountAfterPayment", elementToPayContext.getRemainingAmountAfterPayment());
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void updateFinancialDiscount(ActionRequest request, ActionResponse response) {
    try {
      PayVoucherElementToPay payVoucherElementToPay =
          request.getContext().asType(PayVoucherElementToPay.class);

      Beans.get(PayVoucherElementToPayService.class)
          .updateFinancialDiscount(payVoucherElementToPay);

      response.setValues(payVoucherElementToPay);
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
